home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-03-13 | 13.6 KB | 616 lines | [TEXT/AAIS] |
- /* LMCONV Program: */
- /* */
- /* Use this program to convert Standard */
- /* Prolog syntax programs into the */
- /* Logic Manager syntax. */
- /* */
- /* To run under AAIS Prolog, consult */
- /* this file as well as file with */
- /* desired code (which should not have */
- /* clauses starting with "convert_"). */
- /* Then run "convert_work". */
- /* */
- /* Remember that this conversion program */
- /* has so far only been tested under */
- /* AAIS Prolog. */
-
- /*
- test :- convert_clauses(Defined,fred,0).
- fred :- write('Bratko test 7.3 begin'), nl,
- setof(F1,substitute(sin(x),2*sin(x)*f(sin(x)),t,F1),Z1),
- setof(F2,substitute(a+b,f(a,A+B),v,F2),Z2),
- write('Bratko test 7.3 finished'),nl.
- test :- convert_clauses(Defined,fred,1).
- fred(X) :- (nasty([1,2],[3,4]);fart(1),fart(2),fart(3)).
- test :- convert_clauses(Defined,fred,1).
- fred(X) :- nasty([1,2],[3,4]).
- fred(X) :- asserta(X :- (assertz(lm_trace_on),fail);fail).
- fred(X) :- Q = (assertz(lm_trace_on),fail),asserta(X :- Q).
- fred(X) :- asserta(X :- assertz(lm_trace_on),fail).
- concat([],X,X).
- concat([X|Y],Z,[X|W]) :- concat(Y,Z,W).
- test :- convert_clauses(Defined,lm_trace_backward,2).
- testq(X) :-
- X = Y,
- read(X),
- write(X),
- writel(X),
- nl,
- call(X),
- asserta(X),
- assertz(X),
- !,
- mangle(X,Y,Z),
- compare(X,Y,Z),
- arg(X,Y,Z),
- arity(X,Y),
- name(X,Y),
- '$build'(X,Y),
- var(X),
- atom(X),
- struct(X),
- buff(X),
- int(X),
- const(X),
- nonvar(X),
- X < R,
- X =< R,
- X > R,
- X >= R,
- X == R,
- X \= R,
- functor(X,Y,Z),
- Y is X + 1,
- atomchars(X,Y),
- (call(X);call(Y)),
- length(X,Y),
- not(X),
- findall(X,Y,Z),
- append(X,Y,Z),
- X =.. Y,
- setof(X,Y,Z),
- bagof(X,Y,Z).
- */
-
- convert_work :-
- abolish(convert_last_char,1),
- abolish(convert_names,1),
- telling(T),
- tell(dumpedclauses),
- listing,
- told,
- assert(convert_names([])),
- seeing(Old),
- see(dumpedclauses),
- get0(Y),
- assert(convert_last_char(Y)),
- !,
- convert_scan_file,
- seen,
- seeing(Old),
- retract(convert_names(L)),
- convert_filter(L,LLL),
- sort(LLL,LL),
- assert(convert_count(0)),
- tell(converted),
- !,
- convert_clause_list(LL,LL),
- !,
- nl,
- told.
-
- convert_work_purge :-
- abolish(convert_last_char,1),
- abolish(convert_names,1),
- telling(T),
- tell(dumpedclauses),
- listing,
- told,
- assert(convert_names([])),
- seeing(Old),
- see(dumpedclauses),
- get0(Y),
- assert(convert_last_char(Y)),
- !,
- convert_scan_file,
- seen,
- seeing(Old),
- retract(convert_names(L)),
- convert_filter(L,LL),
- assert(convert_count(0)),
- tell(converted),
- convert_clause_list(LL,LL),
- nl,
- convert_purge(LL),
- told.
-
-
- convert_purge([]).
-
- convert_purge([N,A|R]) :-
- abolish(N,A),
- convert_purge(R).
-
- convert_filter([Name,Arity|R],L) :-
- name(Name,[99,111,110,118,101,114,116,95|_]),
- !,
- convert_filter(R,L).
- convert_filter([Name,Arity|R],[[Name,Arity]|L]) :-
- !,
- convert_filter(R,L).
- convert_filter([],[]).
-
-
- convert_clause_list(Defined,[[Name,Arity]|R]) :-
- convert_clauses(Defined,Name,Arity),
- !,
- (R = [];
- nl),
- convert_clause_list(Defined,R).
-
- convert_clause_list(Defined,[]).
-
- convert_scan_file :-
- repeat,
- convert_some_file,
- convert_last_char(-1),
- !.
-
- convert_some_file :-
- retract(convert_last_char(13)), /* CR */
- get0(X),
- assertz(convert_last_char(X)),
- !.
-
- convert_some_file :- /* name */
- convert_last_char(47),
- get0(42),
- retract(convert_last_char(47)),
- get0(_),
- get0(Q),
- convert_scan_clause_name(Q,T,T,N),
- write(N),
- nl,
- convert_names(L),
- convert_append(L,N,Z),
- assert(convert_names(Z)),
- retract(convert_names(L)),
- get0(X),
- assertz(convert_last_char(X)),
- !.
-
- convert_some_file :- /* skip line */
- convert_skip_line,
- get0(X),
- assertz(convert_last_char(X)),
- !.
-
- convert_some_file :- !. /* EOF */
-
- convert_skip_line :-
- get0(X),
- X >= 0,
- (X = 13;convert_skip_line),
- !.
-
- convert_skip_line :-
- abolish(convert_last_char,1),
- assertz(convert_last_char(-1)),
- !,
- fail.
-
-
- convert_scan_clause_name(Q,Head,R,[Name,Arity]) :-
- convert_name_table(Q),
- Head = [Q|T],
- !,
- get0(C),
- convert_scan_clause_name(C,T,R,[Name,Arity]).
-
- convert_scan_clause_name(Q,Head,R,[Name,Arity]) :-
- Q = 47,
- Head = [],
- name(Name,R),
- convert_scan_arity(Z,Z,Arity).
-
-
- convert_name_table(Q) :-
- Q >= 97,
- Q =< 122.
-
- convert_name_table(Q) :-
- Q >= 48,
- Q =< 57.
-
- convert_name_table(95).
-
- convert_name_table(Q) :-
- Q >= 65,
- Q =< 90.
-
-
- convert_scan_arity(Head,R,Arity) :-
- get0(Q),
- (((Q >= 48,Q =< 57),
- Head = [Q|T],
- !,
- convert_scan_arity(T,R,Arity));
- (Q = 32, Head = [], name(Arity,R))).
-
-
- convert_append([],X,X).
- convert_append([X|Y],Z,[X|W]) :- convert_append(Y,Z,W).
-
-
- convert_clauses(Defined,Symbol,Arity) :-
- functor(Clause,Symbol,Arity),
- clause(Clause,L),
- convert_and_write_clause(Defined,[0|_],0,(Clause:-L)),
- put(46), % 41 is '.'
- nl,
- nl,
- fail.
-
- convert_clauses(Defined,Symbol,Arity).
-
-
- convert_and_write_clause(Defined,Names,T,(Head:-Body)) :-
- convert_do_tabs(T),
- write('rule('),
- TT is T + 1,
- convert_and_write_term(Defined,Names,Head),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Body),
- put(41). % 41 is ')'
-
- convert_and_write_clause(Defined,Names,T,Head) :-
- convert_do_tabs(T),
- write('rule('),
- TT is T + 1,
- !,
- convert_and_write_term(Defined,Names,Head),
- put(44), % 44 is ','
- write(true),
- put(41). % 41 is ')'
-
-
- convert_and_write_body(Defined,Names,T,Goal) :-
- var(Goal),
- convert_do_tabs(T),
- convert_variables(Goal,Names,Name),
- write(Name),
- !.
-
- convert_and_write_body(Defined,Names,T,[Goal1]) :-
- convert_and_write_body(Defined,Names,T,Goal1),
- !.
-
- convert_and_write_body(Defined,Names,T,[Goal1|Goal2]) :-
- convert_do_tabs(T),
- write('and('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal1),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal2),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,','(Goal1,Goal2)) :-
- convert_do_tabs(T),
- write('and('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal1),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal2),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,'->'(C,';'(Goal1,Goal2))) :-
- convert_do_tabs(T),
- write('if_then_else('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,C),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal1),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal2),
- put(41), % 41 is ')'
- convert_warn((C->Goal1;Goal2)).
-
- convert_and_write_body(Defined,Names,T,'->'(C,Goal1)) :-
- convert_do_tabs(T),
- write('if_then('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,C),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal1),
- put(41), % 41 is ')'
- convert_warn((C->Goal1)).
-
- convert_and_write_body(Defined,Names,T,';'(Goal1,Goal2)) :-
- convert_do_tabs(T),
- write('or('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal1),
- put(44), % 44 is ','
- nl,
- !,
- convert_and_write_body(Defined,Names,TT,Goal2),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,!) :-
- convert_do_tabs(T),
- write(cut),
- !.
-
- convert_and_write_body(Defined,Names,T,asserta(C)) :-
- nonvar(C),
- ( C=(_:-_), NC=C ; NC=(C:-true) ),
- convert_do_tabs(T),
- write('asserta('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_clause(Defined,Names,TT,NC),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,assertz(C)) :-
- nonvar(C),
- ( C=(_:-_), NC=C ; NC=(C:-true) ),
- convert_do_tabs(T),
- write('assertz('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_clause(Defined,Names,TT,NC),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,retract(C)) :-
- nonvar(C),
- ( C=(_:-_), NC=C ; NC=(C:-true) ),
- convert_do_tabs(T),
- write('retract('),
- TT is T + 1,
- nl,
- !,
- convert_and_write_clause(Defined,Names,TT,NC),
- put(41). % 41 is ')'
-
- convert_and_write_body(Defined,Names,T,Goal) :-
- convert_do_tabs(T),
- ((atomic(Goal),
- convert_funct_check(Defined,Goal,[],Size,Goal2));
- (\+atomic(Goal),Goal = Goal2)),
- convert_and_write_term(Defined,Names,Goal2),
- !.
-
-
- convert_do_tabs(0).
-
- convert_do_tabs(N) :-
- write(' '),
- NN is N - 1,
- convert_do_tabs(NN).
-
-
- convert_variables(Goal,[I,N,T|R],Name) :-
- II is I + 1,
- R = [II|_],
- (((Goal == N;(var(T),Goal = N)),
- T = Name,
- ((I =< 25,
- X is I + 65,
- name(Name,[X]));
- (I > 25,
- X is 64 + (I // 26),
- Y is 65 + (I mod 26),
- name(Name,[X,Y]))));
- (Goal \== N,
- convert_variables(Goal,R,Name))),
- !.
-
-
- convert_and_write_term(Defined,Names,Goal) :-
- var(Goal),
- convert_variables(Goal,Names,Name),
- write(Name),
- !.
-
- convert_and_write_term(Defined,Names,[]) :-
- write(nil),
- !.
-
- convert_and_write_term(Defined,Names,Goal) :-
- atomic(Goal),
- writeq(Goal),
- !.
-
- convert_and_write_term(Defined,Names,[A|As]) :-
- convert_and_write_term(Defined,Names,cons(A,As)),
- !.
-
- convert_and_write_term(Defined,Names,Goal) :-
- Goal=..[Funct|Args],
- convert_list_length(Args,S),
- ( convert_synonym(Funct,RFunct,S) ; RFunct=Funct ), % check if functor needs renaming
- convert_funct_check(Defined,RFunct,Args,Size,RRFunct),
- ((convert_legal(RRFunct,Size), \+convert_and_quote(RRFunct,Size), write(RRFunct));
- writeq(RRFunct)),
- put(40), % 40 is '('
- !,
- convert_process_args(Names,Args),
- put(41). % 41 is ')'
-
-
- convert_check_functor([F|R],[T|RR]) :-
- ((convert_name_table(F),F=T,convert_check_functor(R,RR));
- (convert_check_functor(R,RRR),
- name(F,N),convert_append(N,RRR,Z),
- name('aSCII',X),convert_append(X,Z,[T|RR]))),
- !.
-
- convert_check_functor([],[]).
-
-
- convert_funct_check(Defined,Funct,Args,Size,Trans) :-
- convert_list_length(Args,Size),
- (convert_legal(Funct,Size),Funct=Trans;
- convert_user_check(Funct,Size,Defined,Trans)),
- !.
-
-
- convert_user_check(F,S,[[Funct,Size]|Defined],Trans) :-
- ((nonvar(Size),F = Funct,S = Size,F = Trans);
- (var(Size),
- ((current_op(_,_,F),name(F,Chars),
- convert_check_functor(Chars,NewChars),
- name(Funct,NewChars),Funct = Trans);
- (\+current_op(_,_,F),F = Funct,S = Size,F = Trans)))),
- !.
-
- convert_user_check(Funct,Size,[[_,S]|Defined],Trans) :-
- nonvar(S),
- convert_user_check(Funct,Size,Defined,Trans).
-
- convert_user_check(Funct,Size,Defined,Funct) :-
- telling(Stream),
- tell(user),
- write('illegal, undefined, or user database functor name: '),
- write(Funct),
- write('/'),
- write(Size),
- nl,
- tell(Stream).
-
-
- convert_list_length([],0).
- convert_list_length([_|R],N) :-
- !,
- convert_list_length(R,NN),
- N is NN + 1.
-
-
- convert_process_args(Names,[A]) :-
- convert_and_write_term(Defined,Names,A),
- !.
-
- convert_process_args(Names,[A|Args]) :-
- convert_and_write_term(Defined,Names,A),
- put(44), % 44 is ','
- !,
- convert_process_args(Names,Args).
-
- convert_process_args(Names,[]) :- !.
-
-
- convert_warn(_).
- /*
- convert_warn(Goal) :-
- telling(T),
- tell(user),
- write('convert_warning: built-in goal'), nl,
- write(Goal), nl,
- write('converted to equivalent LM built-in with different name'), nl,nl,
- told,
- tell(T).
- */
-
-
- convert_legal('true',0). /* not in documentation in LM? */
- convert_legal('fail',0). /* not in documentation in LM? */
- convert_legal('abolish',2). /* not in documentation in LM? */
- convert_legal('clause',2). /* not in documentation in LM? */
- convert_legal('retract',1). /* not documentation and not in LM */
- convert_legal('and',2). convert_synonym(',','and',2).
- convert_legal('or',2). convert_synonym(';','or',2).
-
- convert_legal('=',2).
- convert_legal('eval',2).
- convert_legal('read',1).
- convert_legal('getterm',1).
- convert_legal('write',1).
- convert_legal('writel',1).
- convert_legal('nl',0).
- convert_legal('call',1).
- convert_legal('asserta',1).
- convert_legal('assertz',1).
- convert_legal('cut',0).
- convert_legal('mangle',3).
- convert_legal('compare',3).
- convert_legal('arg',3).
- convert_legal('arity',2).
- convert_legal('name',2). convert_synonym('name','atomchars',2).
- convert_legal('$build',2). convert_and_quote('$build',2).
- convert_legal('var',1).
- convert_legal('atom',1).
- convert_legal('struct',1).
- convert_legal('buff',1).
- convert_legal('integer',1).
- convert_legal('const',1).
- convert_legal('nonvar',1).
- convert_legal('@<',2). convert_synonym('<','@<',2).
- convert_legal('@=<',2). convert_synonym('=<','@=<',2).
- convert_legal('@>',2). convert_synonym('>','@>',2).
- convert_legal('@>=',2). convert_synonym('>=','@>=',2).
- convert_legal('==',2).
- convert_legal('different',2). convert_synonym(X,different,2) :- name(X,[92,61]).
- convert_synonym('!=',different,2). /* is this right? */
- convert_legal('functor',3).
- convert_legal('is',2).
- convert_legal('atomchars',2).
- convert_legal('or',2).
- convert_legal('length',2).
- convert_legal('not',1).
- convert_legal('sort',2).
- convert_legal('copy',2).
- convert_legal('findall',3).
- convert_legal('append',3).
- convert_legal('univ',2). convert_synonym('=..',univ,2).
- convert_legal('setof',3).
- convert_legal('bagof',3).
- convert_legal('add',2). convert_synonym(+,add,2).
- convert_legal('sub',2). convert_synonym(-,sub,2).
- convert_legal('band',2). convert_synonym(/\,band,2).
- convert_legal('lshift',2). convert_synonym(<<,lshift,2).
- convert_legal('rshift',2). convert_synonym(>>,rshift,2).
- convert_legal('bor',2). convert_synonym(\/,bor,2).
- convert_legal('xor',2).
- convert_legal('mul',2). convert_synonym(*,mul,2).
- convert_legal('div',2). convert_synonym(/,div,2).
- convert_legal('idiv',2). convert_synonym(//,idiv,2).
- convert_legal('mod',2).
- convert_legal('xpy',2).
- convert_legal('not',1).
- convert_legal('sqrt',1).
- convert_legal('round',1).
- convert_legal('trunc',1).
- convert_legal('exp',1).
- convert_legal('sin',1).
- convert_legal('cos',1).
- convert_legal('tan',1).
- convert_legal('atan',1).
- convert_legal('log2',1).
- convert_legal('ln',1).
- convert_legal('neg',1).
-
-